package com.lt.demo.webconfig;

import com.lt.demo.controller.ApiResponse;
import io.jsonwebtoken.JwtException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;

@RestControllerAdvice
public class GlobalExceptionHandler {

    // 处理用户身份异常
    @ExceptionHandler(JwtException.class)
    public ApiResponse handleAllExceptions(JwtException ex, WebRequest request) {
        System.out.println(ex);
        return new ApiResponse<>(300, "User identity recognition failed, please log in again ", null);
    }

    // 处理所有未处理的异常
    @ExceptionHandler(Exception.class)
    public ApiResponse handleAllExceptions(Exception ex, WebRequest request) {
        // 这里可以自定义错误信息或使用异常的消息
        ex.printStackTrace();
        return new ApiResponse<>(300, "server internal error", null);
    }
}
